<?php

class AdminController extends \BaseController {
	
	/**
	 * Controller constructor
	 */
	public function __construct() {
		$this->beforeFilter('auth', array('except' => 'postLogin'));
	}
	
	/**
	 * Display a listing of admin users.
	 *
	 * @return Response
	 */
	public function index()
	{
		// get all the admins
		$admins = Admin::all();

		// load the view and pass the admins
		return View::make('admins.index')
			->with('admins', $admins);
	}


	/**
	 * Show the form for creating a new admin user.
	 *
	 * @return Response
	 */
	public function create()
	{
		return View::make('admins.create');
	}


	/**
	 * Store a newly created admin user in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
		// validate
		$rules = array(
			'username'             => 'required|unique:admin',
			'password'             => 'required|confirmed',
			'password_confirmation'=>'required'
		);
		$validator = Validator::make(Input::all(), $rules);

		// process the login
		if ($validator->fails()) {
			return Redirect::to('admins/create')
				->withErrors($validator)
				->withInput(Input::except('password'));
		} else {
			// store
			$admin = new Admin();
			$admin->username       = Input::get('username');
			$admin->password      = Hash::make(Input::get('password'));
			$admin->save();

			// redirect
			Session::flash('message', 'Usuario administrador creado!');
			return Redirect::to('admins');
		}
	}


	/**
	 * Display the specified admin user.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		//
	}


	/**
	 * Show the form for editing the specified admin user.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		// get the nerd
		$admin = Admin::find($id);

		// show the edit form and pass the nerd
		return View::make('admins.edit')
			->with('admin', $admin);
	}


	/**
	 * Update the specified admin user in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		// validate
		$rules = array(
			'username'             => 'required|unique:admin',
			'password'             => 'required|confirmed',
			'password_confirmation'=>'required'
		);
		$validator = Validator::make(Input::all(), $rules);
		
		// process the login
		if ($validator->fails()) {
			return Redirect::to('admins/' . $id . '/edit')
				->withErrors($validator)
				->withInput(Input::except('password'));
		} else {
			// store
			$admin = Admin::find($id);
			$admin->username       = Input::get('username');
			$admin->password      = Hash::make(Input::get('password'));
			$admin->save();

			// redirect
			Session::flash('message', 'Usuario administrador actualizado!');
			return Redirect::to('admins');
		}
	}


	/**
	 * Remove the specified admin user from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		// delete
		$admin = Admin::find($id);
		$admin->delete();

		// redirect
		Session::flash('message', 'Usuario administrador eliminado!');
		return Redirect::to('admins');
	}

	/**
	 * Authenticate and log-in the posted admin user
	 *
	 * @return Response
	 */
	public function postLogin(){
		if (Auth::attempt(array('username'=>Input::get('username'), 'password'=>Input::get('password')), true)) {
			return Redirect::to('/')->with('message', 'Bienvenido nuevamente!');
		} else {
			return Redirect::to('/login')
			->with('message', 'Nombre de usuario o contraseña incorrecto');
		}
	}
	
	/**
	 * Log-out the connected admin user
	 *
	 * @return Response
	 */
	public function getLogout(){
		Auth::logout();
		return Redirect::to('/login')->with('message', 'Ha cerrado su sesión!');
	}
}
